package masterjava.common.logging;

import masterjava.common.configuration.AppSetting;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

/**
 * User: GKislin
 * Date: 29.01.11
 */
public class MasterLogger {
    private final Logger logger;

    private MasterLogger(Class clazz) {
        logger = Logger.getLogger(clazz);
    }

    public static MasterLogger get(Class clazz) {
        return new MasterLogger(clazz);
    }

    public void treatThrowable(String msg, Throwable e) throws Error, RuntimeException, IllegalStateException {
        error(msg, e);
        if (e instanceof Error) {
            throw (Error) e;
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new IllegalStateException(msg, e);
        }
    }

    public static void setRootLoggingLevel(LoggingLevel value) {
        org.apache.log4j.Logger.getRootLogger().setLevel(value.getLevel());
    }

    public IllegalStateException getIllegalStateException(String msg) {
        return getIllegalStateException(msg, null);
    }

    public IllegalStateException getIllegalStateException(String msg, Throwable e) {
        error(msg, e);
        return new IllegalStateException(msg, e);
    }

    public IllegalArgumentException getIllegalArgumentException(String msg) {
        return getIllegalArgumentException(msg, null);
    }

    public IllegalArgumentException getIllegalArgumentException(String msg, Throwable e) {
        error(msg, e);
        return new IllegalArgumentException(msg, e);
    }

    public UnsupportedOperationException getUnsupportedOperationException(String msg) {
        error(msg);
        return new UnsupportedOperationException(msg);
    }

    public static boolean isDebug() {
        return isEnabled(LoggingLevel.DEBUG);
    }

    private static boolean isEnabled(LoggingLevel level) {
        return AppSetting.get().LOGGING_LEVEL.get().ordinal() <= level.ordinal();
    }

    public void debug(CharSequence msg) {
        log(LoggingLevel.DEBUG, msg);
    }

    public void info(CharSequence msg) {
        log(LoggingLevel.INFO, msg);
    }

    public void warn(CharSequence msg) {
        log(LoggingLevel.WARN, msg);
    }

    public void warn(CharSequence msg, Throwable t) {
        log(LoggingLevel.WARN, msg, t);
    }

    public void error(String msg) {
        log(LoggingLevel.ERROR, msg);
    }

    public void error(CharSequence msg, Throwable t) {
        log(LoggingLevel.ERROR, msg, t);
    }

    public void log(LoggingLevel logLevel, CharSequence msg){
        logger.log(logLevel.getLevel(), msg);
    }

    public void log(LoggingLevel logLevel, CharSequence msg, Throwable t){
        logger.log(logLevel.getLevel(), msg, t);
    }

    public static void reset(){
        org.apache.log4j.Logger.getRootLogger().removeAllAppenders();
        BasicConfigurator.configure();
    }
}
